//
//  NewsControler.m
//  Ghac 4S
//
//  Created by apple on 11-8-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "Global.h"
#import "NewsControler.h"
#import "DataFactory.h"
#import "KXML.h"
#import "KXPath.h"
#import "KXMLNode.h"
#import "AccordionNavigator.h"
#import "MyNewsTableCell.h"
#import "RootController.h"
#import "InnerViewController.h"
#import "NewsDetail.h"
#import "RegexKitLite.h"


@implementation NewsControler
@synthesize newsTabelView;
@synthesize mainNavigation;
@synthesize newsView;
@synthesize rssNewsBtn;
@synthesize recentlyNewsBtn;
@synthesize cellDemo;
@synthesize cellBackground;
@synthesize cellSelectedBackground;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        m_dataList123 = Nil;
        m_newsDetail = [[NewsDetail alloc] init];
        [self setTitle:@"news"];
    }
    return self;
}
-(void)onData:(id)sender
{
//    if (m_dic != Nil)
//    {
//        [m_dic release];
//    }

    KXML* xml = [[KXML alloc] initWithData:sender];
    KXPath* path = [[KXPath alloc] initWithString:@"archive" predicate:Nil];
    m_dataList123 = [[NSArray alloc] initWithArray:[xml arrayByXPath:path]];
    [newsTabelView reloadData];
}
-(void)onError:(id)sender
{
    NSLog(@"fail");
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    id<MainViewSwitchAction> target =(id<MainViewSwitchAction>)[([[UIApplication sharedApplication].windows objectAtIndex:0]) rootViewController];
    
    [target showInnerView:m_newsDetail];
    
    if (m_dataList123 == Nil) return;
    KXMLNode* node = [m_dataList123 objectAtIndex:indexPath.row];

    NSString* text = [node valueForKeyPath:@"content.__nodeValue__"];
//    text = [text stringByReplacingOccurrencesOfRegex:@"<[^<>]*>" withString:@""];
//    [m_newsDetail.textView setText:text];
   
    [m_newsDetail.textView setContentToHTMLString:text];

    [m_newsDetail setTitle:[node valueForKeyPath:@"title.__nodeValue__"]];
    [m_newsDetail setImage:[node valueForKeyPath:@"picture.__nodeValue__"]];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"demo"];//:CGRectMake(0, 0, 320, 45)] autorelease];
    MyNewsTableCell* cell = (MyNewsTableCell*)[tableView dequeueReusableCellWithIdentifier:@"demo"];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"MyNewsTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    if (m_dataList123 == Nil) return cell;
    KXMLNode* node = [m_dataList123 objectAtIndex:indexPath.row];
    cell.titleLabel.text = [node valueForKeyPath:@"title.__nodeValue__"];
    cell.dateLabel.text = [node valueForKeyPath:@"crdate.__nodeValue__"];
    return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count =  m_dataList123.count;
    return count;
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self.view addSubview:[MyNavigationController mainNavigationBar].view];
    DataFactory* datafactory = [DataFactory sharedDataFactory];
    id dataProvide = [datafactory produceProviderWithDelegate:self];
    if (dataProvide == Nil)
    {
        return;
    }
    [datafactory askDatas:self.title];
    [newsTabelView setDataSource:self];
    [newsTabelView setDelegate:self];
    
    
    
    
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSDictionary* config = [Global globalConfigDic];
    
    //set for the navigator
    
    CGRect mainFrame = self.view.frame;
    NSArray* titleImageUrlList = [[NSArray alloc] initWithObjects:@"tel_06", @"tel_03", @"tel_06", @"tel_03", nil];
    NSArray* pageViewList = [[NSArray alloc] initWithObjects:self.newsView, nil];
    [self.mainNavigation initWithFrame:self.mainNavigation.frame titleFrameSize:CGSizeMake(self.mainNavigation.frame.size.width, 31)];
    
    UIColor* pageBgColor = [UIColor colorWithRGB:0x2c464c alpha:0.7];
    [mainNavigation setBackgroundColor:pageBgColor];
    
    for (NSInteger i = 0; i < titleImageUrlList.count / 2; i++)
    {
        AccordionTitleView* titleView = [[AccordionTitleView alloc] init];
        UIImage* normalImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[titleImageUrlList objectAtIndex:i * 2] ofType:@"png" ]];
        UIImage* highLighImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[titleImageUrlList objectAtIndex:i * 2 + 1] ofType:@"png" ]];
        
        
        [titleView setImage:normalImage forState:UIControlStateNormal];
        [titleView setImage:highLighImage forState:UIControlStateSelected];
        
        UIView* pageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mainNavigation.frameSize.width, 1000)];
        
        if (pageViewList.count > i) 
        {
            CGRect frame = pageView.frame;
            UIView* pageContentView = [pageViewList objectAtIndex:i];
            frame.size.height = pageContentView.frame.size.height;
            [pageView setFrame:frame];
            [pageContentView setFrame:CGRectMake((mainNavigation.frame.size.width - pageContentView.frame.size.width) / 2, 0, pageContentView.frame.size.width, pageContentView.frame.size.height)];
            [pageView addSubview:pageContentView];

        }
        
        [mainNavigation addPageView:pageView withTitleView:titleView];
        
        [titleView release];
        [pageView release];
    }
    [pageViewList release];
    [titleImageUrlList release];
    [pool drain];
    
    [self.view addSubview:mainNavigation];
    
    
    

}

-(void)newsTypeNavigatorClick:(id)sender
{
    [sender setSelected:YES];
    
    if (sender != recentlyNewsBtn)
    {
        [recentlyNewsBtn setSelected:NO];
    }
    else if (sender != rssNewsBtn)
    {
        [rssNewsBtn setSelected:NO];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.recentlyNewsBtn = nil;
    self.rssNewsBtn = nil;
    self.cellDemo = nil;
    self.newsTabelView = nil;
    self.newsView = nil;
    self.titleButtonDemo = nil;
    self.mainNavigation = nil;
    self.cellBackground = nil;
    self.cellSelectedBackground = nil;
}

-(void)dealloc
{

    [self viewDidUnload];

    self.newsTabelView = Nil;
    [m_dataList123 release];
    [m_newsDetail release];
    [super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
